﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace NetMemosBackground
{
    /// <summary>
    /// OleDB 的摘要说明
    /// </summary>
    public class OleDB
    {
        //private OleDbConnection oleConnection;
        //private OleDbCommand oleCommand;
        //private OleDbDataReader oleReader;
        //private OleDbDataAdapter oleData;
        private string dbPath;
        public string Conn;
        //public OleDB(String dbpath)
        //{
        //    this.dbPath = dbpath;
        //}
        //public OleDbConnection getOleConnect()
        //{
        //    string connectStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbPath + "\\TLDB.accdb";
        //    try
        //    {
        //        oleConnection = new OleDbConnection(connectStr);
        //        oleConnection.Open();
        //    }
        //    catch
        //    {
        //        return null;
        //    }
        //    return oleConnection;
        //}


        //public DataSet getDataset(String sql)
        //{
        //    DataSet ds = new DataSet();
        //    oleConnection = this.getOleConnect();
        //    oleData = new OleDbDataAdapter(sql, oleConnection);
        //    oleData.Fill(ds);
        //    oleConnection.Close();
        //    return ds;
        //}

        //public int executeSql(String sql)
        //{
        //    int count = 0;
        //    oleConnection = this.getOleConnect();
        //    oleCommand = new OleDbCommand(sql, oleConnection);
        //    try
        //    {
        //        count = oleCommand.ExecuteNonQuery();
        //    }
        //    catch (Exception)
        //    {

        //        throw;
        //    }
        //    finally
        //    {
        //        oleConnection.Close();
        //    }
        //    return count;
        //}


        public OleDB()
        {
            getConnString();
        }

        public string getConnString()
        {
            this.Conn = "Database='wtimeline';Server='localhost';User Id='root';Password='820423';Port=3306;charset='utf8';pooling=true;Allow Zero Datetime=True";
            return Conn;
        }

        public DataSet getDataSet(string sql)
        {
            DataSet ds = MySqlHelper.GetDataSet(Conn, CommandType.Text, sql, null);
            return ds;
        }

        public bool executeSql(string sql)
        {
            bool flag = false;
            int i = MySqlHelper.ExecuteNonQuery(Conn, CommandType.Text, sql, null);
            if (i > 0)
            {
                flag = true;
            }
            return flag;
        }

        public object getValue(string query)
        {
            DataSet ds = getDataSet(query);
            return ds.Tables[0].Rows[0][0];
        }


        public int getIntegerValue(String sql)
        {
            int count = 0;
            MySqlDataReader reader = MySqlHelper.ExecuteReader(getConnString(), CommandType.Text, sql, null);
            try
            {
                if (reader.Read())
                    count = reader.GetInt32(0);
            }
            catch (Exception)
            {
                return 0;
            }
            finally
            {
                reader.Close();
            }
            return count;
        }

        public String getStringValue(String sql)
        {
            String temp = "";
            MySqlDataReader reader = MySqlHelper.ExecuteReader(getConnString(), CommandType.Text, sql, null);
            try
            {
                if (reader.Read())
                    temp = reader.GetString(0);
            }
            catch (Exception)
            {
                return "";
            }
            finally
            {
                reader.Close();
            }

            return temp;
        }

        public List<int> getIntegerList(String sql)
        {
            List<int> counts = new List<int>();
            MySqlDataReader reader = MySqlHelper.ExecuteReader(getConnString(), CommandType.Text, sql, null);
            try
            {
                while (reader.Read())
                    counts.Add(reader.GetInt32(0));
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                reader.Close();
            }

            return counts;
        }

        public List<String> getStringList(String sql)
        {
            List<String> temps = new List<String>();
            MySqlDataReader reader = MySqlHelper.ExecuteReader(getConnString(), CommandType.Text, sql, null);
            try
            {
                while (reader.Read())
                    temps.Add(reader.GetString(0));
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                reader.Close();
            }

            return temps;
        }

        public Object getDataValues(String sql)
        {
            Object obj = new Object();
            MySqlDataReader reader = MySqlHelper.ExecuteReader(getConnString(), CommandType.Text, sql, null);
            try
            {
                if (reader.Read())
                    obj = reader.GetValue(0);
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                reader.Close();
            }
            return obj;
        }
    }
}
